import java.util.concurrent.CountDownLatch;

/**\
 * CountDownLatch
 * 闭锁
 * 8个运动员都到达终点，才能结束
 */
public class CountDownLatch1 {
    public static void main(String[] args) throws InterruptedException {
        // 创建闭锁
        CountDownLatch latch = new CountDownLatch(8);
        for (int i = 0; i < 8; i++) {
            int id = i;
            Thread t = new Thread(() -> {
                System.out.println("运动员" + id + "开始");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("运动员" + id + "结束");
                // 减1
                latch.countDown();
            });
            t.start();
        }
        latch.await();
        System.out.println("所有运动员都结束");
    }
}
